from django_filters import rest_framework as filters

from inventory.serializers import LocationSerializer
from inventory.models import Location
from utils.custom_model_viewSet import CustomModelViewSet


class LocationFilter(filters.FilterSet):
    """
    库位过滤类，用于库位列表过滤
    """
    location_code = filters.CharFilter(field_name='location_code', lookup_expr='icontains')
    location_name = filters.CharFilter(field_name='location_name', lookup_expr='icontains')
    warehouse_name = filters.CharFilter(field_name='warehouse__warehouse_name', lookup_expr='icontains')
    
    class Meta:
        model = Location
        fields = [
            'location_id', 'warehouse', 'location_code', 'location_name', 
            'status', 'location_type', 'zone', 'row', 'column', 'level'
        ]


class LocationViewSet(CustomModelViewSet):
    """
    库位视图集，提供库位的增删改查操作
    """
    queryset = Location.objects.filter(is_deleted=False).order_by('warehouse', 'location_code')
    serializer_class = LocationSerializer
    filterset_class = LocationFilter
    search_fields = ['location_code', 'location_name', 'warehouse__warehouse_name']
    ordering_fields = ['location_code', 'warehouse', 'create_time', 'location_id']
    ordering = ['warehouse', 'location_code']
    enable_soft_delete = True